package org.jivesoftware.smackx;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.DiscoverItems;
import org.jivesoftware.smackx.packet.DiscoverItems.Item;
import org.jivesoftware.smackx.packet.OfflineMessageInfo;
import org.jivesoftware.smackx.packet.OfflineMessageRequest;
import org.jivesoftware.smackx.packet.OfflineMessageRequest.Item;

public class OfflineMessageManager
{
  private static final String namespace = "http://jabber.org/protocol/offline";
  private Connection connection;
  private PacketFilter packetFilter;

  public OfflineMessageManager(Connection paramConnection)
  {
    this.connection = paramConnection;
    PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
    arrayOfPacketFilter[0] = new PacketExtensionFilter("offline", "http://jabber.org/protocol/offline");
    arrayOfPacketFilter[1] = new PacketTypeFilter(Message.class);
    this.packetFilter = new AndFilter(arrayOfPacketFilter);
  }

  public void deleteMessages()
    throws XMPPException
  {
    OfflineMessageRequest localOfflineMessageRequest = new OfflineMessageRequest();
    localOfflineMessageRequest.setPurge(true);
    PacketIDFilter localPacketIDFilter = new PacketIDFilter(localOfflineMessageRequest.getPacketID());
    PacketCollector localPacketCollector = this.connection.createPacketCollector(localPacketIDFilter);
    this.connection.sendPacket(localOfflineMessageRequest);
    IQ localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localIQ == null)
      throw new XMPPException("No response from server.");
    if (localIQ.getError() != null)
      throw new XMPPException(localIQ.getError());
  }

  public void deleteMessages(List<String> paramList)
    throws XMPPException
  {
    IQ localIQ;
    OfflineMessageRequest localOfflineMessageRequest = new OfflineMessageRequest();
    Iterator localIterator = paramList.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        PacketIDFilter localPacketIDFilter = new PacketIDFilter(localOfflineMessageRequest.getPacketID());
        PacketCollector localPacketCollector = this.connection.createPacketCollector(localPacketIDFilter);
        this.connection.sendPacket(localOfflineMessageRequest);
        localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
        localPacketCollector.cancel();
        if (localIQ != null)
          break;
        throw new XMPPException("No response from server.");
      }
      OfflineMessageRequest.Item localItem = new OfflineMessageRequest.Item((String)localIterator.next());
      localItem.setAction("remove");
      localOfflineMessageRequest.addItem(localItem);
    }
    if (localIQ.getError() != null)
      throw new XMPPException(localIQ.getError());
  }

  public Iterator<OfflineMessageHeader> getHeaders()
    throws XMPPException
  {
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = ServiceDiscoveryManager.getInstanceFor(this.connection).discoverItems(null, "http://jabber.org/protocol/offline").getItems();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return localArrayList.iterator();
      localArrayList.add(new OfflineMessageHeader((DiscoverItems.Item)localIterator.next()));
    }
  }

  public int getMessageCount()
    throws XMPPException
  {
    int i;
    Form localForm = Form.getFormFrom(ServiceDiscoveryManager.getInstanceFor(this.connection).discoverInfo(null, "http://jabber.org/protocol/offline"));
    if (localForm != null)
      i = Integer.parseInt((String)localForm.getField("number_of_messages").getValues().next());
    while (true)
    {
      return i;
      i = 0;
    }
  }

  public Iterator<Message> getMessages()
    throws XMPPException
  {
    ArrayList localArrayList = new ArrayList();
    OfflineMessageRequest localOfflineMessageRequest = new OfflineMessageRequest();
    localOfflineMessageRequest.setFetch(true);
    PacketIDFilter localPacketIDFilter = new PacketIDFilter(localOfflineMessageRequest.getPacketID());
    PacketCollector localPacketCollector1 = this.connection.createPacketCollector(localPacketIDFilter);
    PacketCollector localPacketCollector2 = this.connection.createPacketCollector(this.packetFilter);
    this.connection.sendPacket(localOfflineMessageRequest);
    IQ localIQ = (IQ)localPacketCollector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector1.cancel();
    if (localIQ == null)
      throw new XMPPException("No response from server.");
    if (localIQ.getError() != null)
      throw new XMPPException(localIQ.getError());
    Message localMessage = (Message)localPacketCollector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    while (true)
    {
      if (localMessage == null)
      {
        localPacketCollector2.cancel();
        return localArrayList.iterator();
      }
      localArrayList.add(localMessage);
      localMessage = (Message)localPacketCollector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    }
  }

  public Iterator<Message> getMessages(List<String> paramList)
    throws XMPPException
  {
    PacketCollector localPacketCollector2;
    IQ localIQ;
    ArrayList localArrayList = new ArrayList();
    OfflineMessageRequest localOfflineMessageRequest = new OfflineMessageRequest();
    Iterator localIterator = paramList.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        PacketIDFilter localPacketIDFilter = new PacketIDFilter(localOfflineMessageRequest.getPacketID());
        PacketCollector localPacketCollector1 = this.connection.createPacketCollector(localPacketIDFilter);
        PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
        arrayOfPacketFilter[0] = this.packetFilter;
        arrayOfPacketFilter[1] = new PacketFilter(this, paramList)
        {
          public boolean accept()
          {
            OfflineMessageInfo localOfflineMessageInfo = (OfflineMessageInfo)paramPacket.getExtension("offline", "http://jabber.org/protocol/offline");
            return this.val$nodes.contains(localOfflineMessageInfo.getNode());
          }
        };
        AndFilter localAndFilter = new AndFilter(arrayOfPacketFilter);
        localPacketCollector2 = this.connection.createPacketCollector(localAndFilter);
        this.connection.sendPacket(localOfflineMessageRequest);
        localIQ = (IQ)localPacketCollector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
        localPacketCollector1.cancel();
        if (localIQ != null)
          break;
        throw new XMPPException("No response from server.");
      }
      OfflineMessageRequest.Item localItem = new OfflineMessageRequest.Item((String)localIterator.next());
      localItem.setAction("view");
      localOfflineMessageRequest.addItem(localItem);
    }
    if (localIQ.getError() != null)
      throw new XMPPException(localIQ.getError());
    Message localMessage = (Message)localPacketCollector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    while (true)
    {
      if (localMessage == null)
      {
        localPacketCollector2.cancel();
        return localArrayList.iterator();
      }
      localArrayList.add(localMessage);
      localMessage = (Message)localPacketCollector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    }
  }

  public boolean supportsFlexibleRetrieval()
    throws XMPPException
  {
    return ServiceDiscoveryManager.getInstanceFor(this.connection).discoverInfo(null).containsFeature("http://jabber.org/protocol/offline");
  }
}